var async = require('async');
var moment = require('moment');

var connPool = require('./connPool.js');
var LoginBean = require('../jsBean/LoginBean');
var Result = require('../jsBean/Result');

module.exports = {
	// 进入新闻具体内容页
	newsContent: function (req, res, loginbean) {
		var newsId = req.query['newsId'];
		
		// 转换时间戳格式
		function timeTransform (time) {
			return moment(time).format('YYYY-MM-DD HH:mm:ss');
		}
		
		var pool = connPool();
	
		pool.getConnection(function (err, conn) {
			if(err) {
				console.log(err.message);
				
				return;
			}

			async.waterfall ([
				
				// 查询新闻内容
				function(done) {
					var selectNewsSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.id=?;';
					var param = [newsId];
					
					conn.query(selectNewsSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						var newsContent = rs[0];
						newsContent.sendTime = timeTransform(newsContent.sendTime);
						
						var data = {};
						
						data.newsContent = newsContent;
						
						done(null, data);
					});
				},
				
				// 查询推荐内容
				function(data, done) {
					var selectNewsSql = 'SELECT id,title FROM news WHERE classify=? ORDER BY click DESC LIMIT 6;';
					var param = [data.newsContent.classify];
					
					conn.query(selectNewsSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						data.recommendNews = rs;
						
						done(null, data);
					});
				},
				
				// 更新新闻点击量
				function(data, done) {
					var updateNewsSql = 'UPDATE news SET click=click+1 WHERE id=?;';
					var param = [newsId];
					
					conn.query(updateNewsSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, data);
					});
				},
				
				// 查询用户是否收藏
				function(data, done) {
					if(loginbean) {
						var selectNewsSql = 'SELECT id FROM userCollect WHERE userId=? AND newsId=?;';
						var param = [loginbean.id, newsId];
						
						conn.query(selectNewsSql, param, function (err, rs) {
							if(err) {
								console.log(err.message);
								
								return;
							}
							
							data.userCollect = rs[0];
							done(null, data);
						});
					} else {
						done(null, data);	
					}
				},
				
				// 查询0级评论
				function(data, done) {
					var selectCommentSql = 'SELECT c.*, u.userPicture AS userPicture, u.name AS sendName From comment AS c JOIN user AS u ON c.commentId=u.id WHERE c.newsId=? AND c.commentLevels=? ORDER BY c.sendTime DESC;';
					var param = [newsId, '主'];
					
					conn.query(selectCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						var newsCommentMain = rs;
						newsCommentMain.forEach(function (item) {
							item.sendTime = timeTransform(item.sendTime);
						});

						data.newsCommentMain = newsCommentMain;
						done(null, data);
					});
				},
				
				// 查询1,2级评论
				function(data, done) {
					var selectCommentSql = 'SELECT c.*, u.userPicture AS userPicture, u.name AS sendName, us.name AS beCommentName From comment AS c JOIN user AS u ON c.commentId=u.id JOIN user AS us ON c.beCommentId=us.id WHERE c.newsId=? AND c.commentLevels=?;';
					var param = [newsId, '从'];
					
					conn.query(selectCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						var newsCommentLess = rs;
						newsCommentLess.forEach(function (item) {
							item.sendTime = timeTransform(item.sendTime);
						});

						data.newsCommentLess = newsCommentLess;
						done(null, data);
					});
				}
				
			], function (err, data) {
				if(err) {
					conn.release();
					console.log(err);
					
					return;
				}
				
				conn.release();
				
				res.render('newsContent', {
					loginbean: loginbean,
					newsContent: data.newsContent,
					newsCommentMain: data.newsCommentMain,
					newsCommentLess: data.newsCommentLess,
					userCollect: data.userCollect,
					recommendNews: data.recommendNews
				});
			});
			 
		});
	},
	
	// 提交0级评论
	newsComment: function (req, res, loginbean) {
		var content = req.body['formValues'];
		var newsId = req.body['newsId'];
		
		// 转换时间戳格式
		function timeTransform (time) {
			return moment(time).format('YYYY-MM-DD HH:mm:ss');
		}
		
		var pool = connPool();
	
		pool.getConnection(function (err, conn) {
			if(err) {
				console.log(err.message);
				
				return;
			}
			
			async.waterfall ([
				
				function(done) {
					var selectUserSql = 'SELECT state FROM user WHERE id=?;';
					var param = [loginbean.id];
					
					conn.query(selectUserSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						var err = null;
						var data = {};
						
						if(rs[0].state == "禁言") {
							err = "您已被禁言！";
						}
						done(err, data);
					});
				},
				
				function(data, done) {
					var insertCommentSql = 'INSERT comment(content, newsId, commentId, beCommentId) VALUES(?, ?, ?, ?);';
					var param = [content, newsId, loginbean.id, loginbean.id];
					
					conn.query(insertCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						data.content = content;
						
						done(null, data);
					});
					
				},
				
				// 更新新闻评论量
				function(data, done) {
					var updateNewsSql = 'UPDATE news SET comment=comment+1 WHERE id=?;';
					var param = [newsId];
					
					conn.query(updateNewsSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, data);
					});
				},

				function(data, done) {
					var selectCommentSql = 'SELECT id, sendTime FROM comment WHERE commentId=? AND newsId=? ORDER BY sendTime DESC LIMIT 1;';
					var param = [loginbean.id, newsId];
					
					conn.query(selectCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						data.sendTime = rs[0].sendTime;
						data.sendTime = timeTransform(data.sendTime);
						data.id = rs[0].id;
						done(null, data);
					});
				},
				
				function(data, done) {
					data.sendName = loginbean.name;
					data.commentId = loginbean.id;
					data.userPicture = loginbean.userPicture;
					done(null, data);
				}
				
				
			], function(err, rs) {
				if(err) {
					var result = new Result();
					result.status = 0;
					result.data = {
						message: err
					};
					
					res.send(result);
					conn.release();
					return;
				}
				
				conn.release();
				
				var result = new Result();
				result.status = 1;
				result.data = rs;
				
				res.send(result);
			});
			 
		});
	},
	
	// 提交1,2级评论
	newsCommentOneTwo: function (req, res, loginbean) {
		var content = req.body['content'];
		var newsId = req.body['newsId'];
		var beCommentId = req.body['beCommentId'];
		var mainCommentObjectId = req.body['mainCommentObjectId'];
		var beCommentName = req.body['beCommentName'];
		
		// 转换时间戳格式
		function timeTransform (time) {
			return moment(time).format('YYYY-MM-DD HH:mm:ss');
		}
		
		var pool = connPool();
	
		pool.getConnection(function (err, conn) {
			if(err) {
				console.log(err.message);
				
				return;
			}
			
			async.waterfall ([
				
				function(done) {
					var selectUserSql = 'SELECT state FROM user WHERE id=?;';
					var param = [loginbean.id];
					
					conn.query(selectUserSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						var err = null;
						var data = {};
						
						if(rs[0].state == "禁言") {
							err = "您已被禁言！";
						}
						done(err, data);
					});
				},
				
				function(data, done) {
					var insertCommentSql = 'INSERT comment(content, newsId, commentId, beCommentId, commentLevels, mainCommentObjectId) VALUES(?, ?, ?, ?, ?, ?);';
					var param = [content, newsId, loginbean.id, beCommentId, '从', mainCommentObjectId];
					
					conn.query(insertCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
 						
					});
					
					data.content = content;
					data.beCommentName = beCommentName;
					
					done(null, data);
				},

				// 更新新闻评论量
				function(data, done) {
					var updateNewsSql = 'UPDATE news SET comment=comment+1 WHERE id=?;';
					var param = [newsId];
					
					conn.query(updateNewsSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, data);
					});
				},

				function(data, done) {
					var selectCommentSql = 'SELECT sendTime FROM comment WHERE commentId=? AND beCommentId=? ORDER BY sendTime DESC LIMIT 1;';
					var param = [loginbean.id, beCommentId];
					
					conn.query(selectCommentSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						data.sendTime = rs[0].sendTime;
						data.sendTime = timeTransform(data.sendTime);
						done(null, data);
					});
				},
				
				function(data, done) {
					data.sendName = loginbean.name;
					data.commentId = loginbean.id;
					data.userPicture = loginbean.userPicture;
					done(null, data);
				}
				
				
			], function(err, rs) {
				if(err) {
					var result = new Result();
					result.status = 0;
					result.data = {
						message: err
					};
					
					res.send(result);
					conn.release();
					return;
				}
				
				conn.release();
				
				var result = new Result();
				result.status = 1;
				result.data = rs;
				
				res.send(result);
			});
			 
		});
	}
};